package com.hrt.manage.bkgoods.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.hrt.base.BaseDaoImpl;
import com.hrt.bean.BackGoods;
import com.hrt.manage.bkgoods.dao.BackGoodsDao;

@Repository
public class BackGoodsDaoImpl extends BaseDaoImpl<BackGoods> implements
		BackGoodsDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<BackGoods> queryByOrderId(Long orderId) {
		String hql = "select bg from BackGoods bg where bg.orderId='" + orderId
				+ "' and bg.isdelete=0";

		return getHibernateTemplate().find(hql);
	}

	@Override
	public Long countOrderBackGoodes(Long giftDetailId, Long orderId) {
		final String hql = "select count(bg.id) from BackGoods bg where bg.orderId="
				+ orderId
				+ " and bg.isdelete=0 and bg.giftDetailId="
				+ giftDetailId+" GROUP BY bg.giftDetailId";

		return getHibernateTemplate().execute(new HibernateCallback<Long>() {

			@Override
			public Long doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				if(query.uniqueResult()==null){
					return 0l;
				}
				return (Long) query.uniqueResult();
			}

		});
	}

}
